/**
 * lilang
 * BaseServiceImpl.java
 * 2013-6-15上午9:53:02
 */
package oa.base.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.base.hibernate.BaseClass;
import oa.base.hibernate.EntityHibernateDao;
import oa.base.service.BaseService;
import oa.base.util.BaseConstants;
import oa.base.util.BeanUtils;
import oa.base.util.Constants;
import oa.base.util.Page;

/**
 * @author lilang
 * @param <E>
 *
 */
public class BaseServiceImpl<T extends BaseClass> implements BaseService<T>{

	
	
	
	private EntityHibernateDao<T> entityHibernateDao;
	
	
	
	public void setEntityHibernateDao(EntityHibernateDao<T> entityHibernateDao) {
		this.entityHibernateDao = entityHibernateDao;
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#create(oa.base.hibernate.BaseClass)
	 */
	public Long create(T t) {
		return (Long) entityHibernateDao.create(t);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#update(oa.base.hibernate.BaseClass)
	 */
	public void update(T t) {
		entityHibernateDao.update(t);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#delete(oa.base.hibernate.T)
	 */
	public void delete(T t) {
		entityHibernateDao.remove(t);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findParByList(java.util.Map)
	 */
	public List<T> findParByList(Map<String, Object> params) {
		return entityHibernateDao.find(params);
	}
	
	public List<T> findParByList(Map<String,Object> params,Map<String,Object> likeMap,Map<String,Object> sortMap){
		return entityHibernateDao.find(params, likeMap, sortMap);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findUnObject(java.util.Map)
	 */
	public T findUnObject(Map<String, Object> params) {
		return entityHibernateDao.getUniqueBy(params);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findByPage(java.util.Map, java.util.Map, java.util.Map, int, int)
	 */
	@SuppressWarnings("unchecked")
	public Page<T> findByPage(Map<String, Object> filterMap,
			Map<String, Object> likeMap, Map<String, Object> sortMap,
			int pageNo, int pageSize) {
		return entityHibernateDao.pagedQuery(filterMap, likeMap, sortMap, pageNo, pageSize);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#updateExcute(java.lang.String)
	 */
	public void updateExcute(String sql) {
		this.entityHibernateDao.updateExecutSQL(sql);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#updateSelect(oa.base.hibernate.BaseClass)
	 */
	public void updateSelect(T t) {
		this.entityHibernateDao.updateSelect(t);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#createAll(java.util.Collection)
	 */
	public int createAll(Collection<T> list) {
		this.entityHibernateDao.saveAll(list);
		return 0;
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#saveAll(java.util.Collection)
	 */
	public void saveAll(Collection<T> list) {
		this.entityHibernateDao.saveAll(list);
	}
	
	public void deleteAll(Collection<T> list){
		this.entityHibernateDao.removeAll(list);
	}

	public void delete(Long sid) {
		this.entityHibernateDao.removeById(sid);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findByParmkeys(java.lang.Long)
	 */
	public T findByParmkeys(Long sid) {
		return this.entityHibernateDao.get(sid);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#updateAll(java.util.Collection)
	 */
	public void updateAll(Collection<T> list) {
		Map<Long,T> param = new HashMap<Long,T>();
		List<Long> sids = new ArrayList<Long>();
		for(T it:list){
			if(null!=it.getSid()){
				sids.add(it.getSid());
				param.put(it.getSid(), it);
			}
		}
		Map<String,Object> filter = new HashMap<String,Object>();
		filter.put(BaseConstants.SID+Constants.WHERE_IN, sids);
		List<T> collecon=this.entityHibernateDao.find(filter);
		for(T it:collecon){
			T souce = param.get(it.getSid());
			if(souce!=null){
				BeanUtils.copyPropertiesNoNull(souce, it);
			}
		}
		this.saveAll(collecon);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#deleteById(java.util.List)
	 */
	public void deleteById(List<Long> ids) {
		Map<String,Object> filter = new HashMap<String,Object>();
		filter.put(BaseConstants.SID+Constants.WHERE_IN, ids);
		List<T> collecon=this.entityHibernateDao.find(filter);
		this.deleteAll(collecon);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#delete(java.util.Map)
	 */
	public void delete(Map<String, Object> maps) {
		this.entityHibernateDao.remove(maps);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findParByList(java.util.List)
	 */
	public List<T> findParByList(List<Long> sids) {
		Map<String,Object> filter = new HashMap<String, Object>();
		filter.put(Constants.WHERE_IN+"sid", sids);
		return this.entityHibernateDao.find(filter);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findByParamCount(java.util.Map)
	 */
	public Long findByParamCount(Map<String, Object> params) {
		return this.entityHibernateDao.findByCount(params, null);
	}

	/* (non-Javadoc)
	 * @see oa.base.service.BaseService#findByParams(java.util.Map, java.util.Map)
	 */
	public List<T> findByParams(Map<String, Object> params,
			Map<String, Object> orders) {
		return this.entityHibernateDao.find(params, orders);
	}
	
	public boolean isUnique(Object entity,String names){
		return this.entityHibernateDao.isUnique(entity, names);
	}
}
